package com.kerpow.KSheet;

import java.io.File;

import com.kerpow.KSheet.character.CharacterAdapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SheetListItem extends FrameLayout {
	private File _file;
	
	private OnSheetItemClickHandler _OnSheetItemClickHandler;
	
	private ImageButton _refreshButton;
	private ImageButton _clearButton;
	
	private CharacterAdapter.LoadState _loadState;

	
	public interface OnSheetItemClickHandler{
		public void OnOpenSheet(File f, CharacterAdapter.LoadState state);
		public void OnRefreshSheet(File f);
		public void OnClearData(File f);
	}
	
	public SheetListItem(File f, CharacterAdapter.LoadState loadState, Context context) {
		super(context);
		_loadState = loadState;
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.sheetlistitem, null);	
		
		OnClickListener click = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(_OnSheetItemClickHandler != null) _OnSheetItemClickHandler.OnOpenSheet(_file, _loadState);
				SheetListItem.this.setBackgroundColor(Color.GRAY);
			}
		};
		
		
		TextView tv;
		
		
		tv = (TextView) ll.findViewById(R.id.filename);
		
		tv.setText(f.getName().replace(".dnd4e", ""));
		tv.setOnClickListener(click);
		
		tv = (TextView) ll.findViewById(R.id.filedetail);
		tv.setOnClickListener(click);
		String msg="";
		
		_clearButton = (ImageButton)ll.findViewById(R.id.cleardata);
		_refreshButton  = (ImageButton)ll.findViewById(R.id.refreshdata);
		
		switch(loadState){
		case MustReload:
			msg = context.getString(R.string.file_status_reload);
			_clearButton.setVisibility(View.VISIBLE);
			break;
		case CannotReload:
			msg = context.getString(R.string.file_status_connotreload);
			_clearButton.setVisibility(View.VISIBLE);
			break;
		case Unkown:
			msg = context.getString(R.string.file_status_load);
			break;
		case Ready:
			msg = context.getString(R.string.file_status_ready);
			_clearButton.setVisibility(View.VISIBLE);
			_refreshButton.setVisibility(View.VISIBLE);
			break;
		}
		
		tv.setText(msg);
		
		_file = f;
		
		this.addView(ll);
		
		
		
		this.setOnClickListener(click);
		
		_refreshButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(_OnSheetItemClickHandler != null) _OnSheetItemClickHandler.OnRefreshSheet(_file);
			}
		});
		_clearButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(_OnSheetItemClickHandler != null) _OnSheetItemClickHandler.OnClearData(_file);
			}
		});
	}
	

	public void setOnSheetItemClickHandler(OnSheetItemClickHandler handler){
		_OnSheetItemClickHandler = handler;
	}
	

}
